package com.example.stuwork.service.impl;

import com.example.stuwork.common.Result;
import com.example.stuwork.dao.CourseAnnouncementDao;
import com.example.stuwork.pojo.CourseAnnouncement;
import com.example.stuwork.service.CourseAnnouncementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课程公告服务实现类
 */
@Service
public class CourseAnnouncementServiceImpl implements CourseAnnouncementService {

    @Autowired
    private CourseAnnouncementDao courseAnnouncementDao;

    /**
     * 获取所有课程公告
     *
     * @return 课程公告列表
     */
    @Override
    public Result<List<CourseAnnouncement>> getAllAnnouncements() {
        try {
            List<CourseAnnouncement> announcements = courseAnnouncementDao.findAll();
            return Result.success("获取课程公告列表成功", announcements);
        } catch (Exception e) {
            return Result.error("获取课程公告列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取课程公告
     *
     * @param announcementId 公告ID
     * @return 课程公告
     */
    @Override
    public Result<CourseAnnouncement> getAnnouncementById(Long announcementId) {
        try {
            CourseAnnouncement announcement = courseAnnouncementDao.getById(announcementId);
            if (announcement != null) {
                return Result.success("获取课程公告成功", announcement);
            } else {
                return Result.error("课程公告不存在");
            }
        } catch (Exception e) {
            return Result.error("获取课程公告失败：" + e.getMessage());
        }
    }

    /**
     * 根据课程ID和班级ID获取课程公告
     *
     * @param courseId 课程ID
     * @param classId 班级ID
     * @return 课程公告列表
     */
    @Override
    public Result<List<CourseAnnouncement>> getAnnouncementsByCourseAndClass(String courseId, Long classId) {
        try {
            List<CourseAnnouncement> announcements = courseAnnouncementDao.findByCourseAndClass(courseId, classId);
            return Result.success("获取课程公告列表成功", announcements);
        } catch (Exception e) {
            return Result.error("获取课程公告列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据教师ID获取课程公告
     *
     * @param teacherId 教师ID
     * @return 课程公告列表
     */
    @Override
    public Result<List<CourseAnnouncement>> getAnnouncementsByTeacherId(String teacherId) {
        try {
            List<CourseAnnouncement> announcements = courseAnnouncementDao.findByTeacherId(teacherId);
            return Result.success("获取教师课程公告列表成功", announcements);
        } catch (Exception e) {
            return Result.error("获取教师课程公告列表失败：" + e.getMessage());
        }
    }

    /**
     * 添加课程公告
     *
     * @param announcement 课程公告
     * @return 添加结果
     */
    @Override
    @Transactional
    public Result<?> addAnnouncement(CourseAnnouncement announcement) {
        try {
            int result = courseAnnouncementDao.insert(announcement);
            if (result > 0) {
                return Result.success("添加课程公告成功", announcement.getAnnouncementId());
            } else {
                return Result.error("添加课程公告失败");
            }
        } catch (Exception e) {
            return Result.error("添加课程公告失败：" + e.getMessage());
        }
    }

    /**
     * 更新课程公告
     *
     * @param announcementId 公告ID
     * @param announcement 课程公告
     * @return 更新结果
     */
    @Override
    @Transactional
    public Result<?> updateAnnouncement(Long announcementId, CourseAnnouncement announcement) {
        try {
            // 先查询原有公告，确保存在
            CourseAnnouncement existing = courseAnnouncementDao.getById(announcementId);
            if (existing == null) {
                return Result.error("课程公告不存在");
            }
            
            // 设置ID并更新
            announcement.setAnnouncementId(announcementId);
            int result = courseAnnouncementDao.update(announcement);
            
            if (result > 0) {
                return Result.success("更新课程公告成功");
            } else {
                return Result.error("更新课程公告失败");
            }
        } catch (Exception e) {
            return Result.error("更新课程公告失败：" + e.getMessage());
        }
    }

    /**
     * 删除课程公告
     *
     * @param announcementId 公告ID
     * @return 删除结果
     */
    @Override
    @Transactional
    public Result<?> deleteAnnouncement(Long announcementId) {
        try {
            // 先查询原有公告，确保存在
            CourseAnnouncement existing = courseAnnouncementDao.getById(announcementId);
            if (existing == null) {
                return Result.error("课程公告不存在");
            }
            
            int result = courseAnnouncementDao.delete(announcementId);
            
            if (result > 0) {
                return Result.success("删除课程公告成功");
            } else {
                return Result.error("删除课程公告失败");
            }
        } catch (Exception e) {
            return Result.error("删除课程公告失败：" + e.getMessage());
        }
    }
} 